<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>淘贝商城 - 购物车</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <style>
        .navbar-default {
            background-color: #FFA500; /* 橙黄色 */
            border-color: #E69500;
        }
        .navbar-default .navbar-brand,
        .navbar-default .navbar-nav > li > a {
            color: #FFF; /* 白色文字 */
        }
        .navbar-default .navbar-nav > .active > a,
        .navbar-default .navbar-nav > .active > a:hover,
        .navbar-default .navbar-nav > .active > a:focus {
            background-color: #E69500; /* 深一点的橙黄色 */
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">淘贝商城</a>
            </div>
        </div>
    </nav>

    <div class="container">
        <h1>购物车</h1>
        <table class="table">
            <thead>
                <tr>
                    <th>商品</th>
                    <th>图片</th>
                    <th>价格</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="cart-items">
                <!-- 购物车商品动态渲染 -->
            </tbody>
        </table>
        <a href="payment.html" class="btn btn-success">付款</a>
        <a href="..\index.html" class="btn btn-link">返回主页</a>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script>
        // 渲染购物车商品
        document.addEventListener('DOMContentLoaded', function () {
            const cart = JSON.parse(localStorage.getItem('cart')) || [];
            const cartItemsContainer = document.getElementById('cart-items');
            cartItemsContainer.innerHTML = cart.map(item => `
                <tr>
                    <td>${item.name}</td>
                    <td><img src="${item.image}" alt="${item.name}" width="50"></td>
                    <td>￥${item.price}</td>
                    <td><button class="btn btn-danger remove-item">删除</button></td>
                </tr>
            `).join('');

            // 删除商品功能
            const removeButtons = document.querySelectorAll('.remove-item');
            removeButtons.forEach(button => {
                button.addEventListener('click', function () {
                    const itemName = button.closest('tr').querySelector('td').innerText;
                    removeFromCart(itemName);
                    location.reload(); // 刷新页面
                });
            });

            function removeFromCart(name) {
                let cart = JSON.parse(localStorage.getItem('cart')) || [];
                cart = cart.filter(item => item.name !== name);
                localStorage.setItem('cart', JSON.stringify(cart));
            }
        });
    </script>
</body>
</html>